package cn.enilu.flash.api.controller.mini;

import cn.enilu.flash.api.controller.BaseController;
import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.entity.house.RepairFile;
import cn.enilu.flash.bean.entity.house.RepairInfo;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.enums.body.PayState;
import cn.enilu.flash.enums.body.RepairFileType;
import cn.enilu.flash.enums.body.RepairState;
import cn.enilu.flash.security.JwtUtil;
import cn.enilu.flash.service.house.RepairFileService;
import cn.enilu.flash.service.house.RepairInfoService;
import cn.enilu.flash.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Author Spark
 * @Date Created in (2020/04/27 11:17)
 * @Version v1.0
 * @Modify
 **/
@Slf4j
@RestController
@RequestMapping(value = "/mini/repair", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RepairController extends BaseController {

    @Autowired
    private RepairInfoService repairInfoService;
    @Autowired
    private RepairFileService repairFileService;

    @PostMapping(value = "insert")
    @BussinessLog(value = "新增客户报修申请", key = "mobile",dict= CommonDict.class)
    public Object insert(@RequestParam(value = "communityId") Long communityId,
                         @RequestParam(value = "buildingId") Long buildingId,
                         @RequestParam(value = "roomId") Long roomId,
                         @RequestParam(value = "mobile") String mobile,
                         @RequestParam(value = "applyName") String applyName,
                         @RequestParam(value = "applyMobile") String applyMobile,
                         @RequestParam(value = "applyType") Integer applyType,
                         @RequestParam(value = "applyContent", required = false) String applyContent,
                         @RequestParam(value = "files", required = false) String files){
        Long[] fileIds;
        Long custId = JwtUtil.getCustId();
        if(custId == null || custId == 0){
            return Rets.failure("令牌无效");
        }
        String tokenMobile = JwtUtil.getCustMobile();
        if(tokenMobile == null || !StringUtils.equals(tokenMobile, mobile)){
            return Rets.failure("用户令牌已被串改");
        }
        if(StringUtils.isNotNullOrEmpty(files)){
            String[] ids = files.split(",");
            if(ids.length > 3){
                return Rets.failure("最多只能上传三张图片");
            }else{
                fileIds = new Long[ids.length];
                try {
                    for (int i = 0; i < ids.length; i++) {
                        fileIds[i] = Long.parseLong(ids[i]);
                    }
                }catch (NumberFormatException e){
                    return Rets.failure("传入的文件ID不合法");
                }
            }
        }else{
            fileIds = new Long[]{};
        }

        RepairInfo info = new RepairInfo();
        info.setCode(StringUtils.getReqSnNo("BX"));
        info.setCommunityId(communityId);
        info.setBuildingId(buildingId);
        info.setRoomId(roomId);
        info.setCustId(custId);
        info.setApplyName(applyName);
        info.setApplyMobile(applyMobile);
        info.setApplyType(applyType);
        info.setApplyContent(applyContent);
        info.setState(RepairState.WAITING.getCode());
        info.setFeeState(PayState.UNPAID.getCode());
        RepairInfo temp = repairInfoService.insert(info);

        RepairFile rf = null;
        for(Long fileId : fileIds){
            rf = new RepairFile();
            rf.setRepairId(temp.getId());
            rf.setType(RepairFileType.APPLY.getCode());
            rf.setFileId(fileId);
            repairFileService.insert(rf);
        }

        return Rets.success();
    }
}
